In a previous analysis, I explored publicly available data from the Bureau of Meteorology to examine change in Australian temperatures. Here, we extend upon this data using gganimate to create three dynamic plots
Let’s load our dependencies
require(tidyverse)
require(readr)
require(ggplot2)
require(gganimate)
require(viridis)
require(scales)
require(gifski)
require(png)
require(transformr)
require(kableExtra)
& read-in the pre-cleaned temperature data from the previous kernel
tbl <- read.csv("ACORN-SAT-Clean.csv")
| location2 | site.name | date | minimum.temperature..degC. | maximum.temperature..degC. | Year | Month | Week | Season | Yr_Month | Yr_Week | Era | lat | long | elev | State |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1019 | KALUMBURU | 1941-09-01 | 20.6 | 29.8 | 1941 | 9 | 36 | Spring | 1941-09 | 1941-36 | 1940s | -14.3 | 126.65 | 23 | WA |
| 1019 | KALUMBURU | 1941-09-02 | 20.6 | 29.8 | 1941 | 9 | 36 | Spring | 1941-09 | 1941-36 | 1940s | -14.3 | 126.65 | 23 | WA |
| 1019 | KALUMBURU | 1941-09-03 | 19.6 | 29.3 | 1941 | 9 | 36 | Spring | 1941-09 | 1941-36 | 1940s | -14.3 | 126.65 | 23 | WA |
| 1019 | KALUMBURU | 1941-09-04 | 21.2 | 37.1 | 1941 | 9 | 36 | Spring | 1941-09 | 1941-36 | 1940s | -14.3 | 126.65 | 23 | WA |
| 1019 | KALUMBURU | 1941-09-05 | 19.8 | 30.9 | 1941 | 9 | 36 | Spring | 1941-09 | 1941-36 | 1940s | -14.3 | 126.65 | 23 | WA |
| 1019 | KALUMBURU | 1941-09-06 | 19.8 | NA | 1941 | 9 | 36 | Spring | 1941-09 | 1941-36 | 1940s | -14.3 | 126.65 | 23 | WA |
Before beginning, if using a mac, you may want to consult this resource to do some required set-up in Terminal. Otherwise, we should be good-to-go.
In one piece of code, lets aggregate the data & create our plot
# Minimalistic theme for visualisation
theme_plot_text <-
theme(
plot.title = element_text(size = 10, hjust = 0.5),
axis.title = element_text(size = 10),
legend.title = element_text(size = 10),
axis.text = element_text(size = 7))
# plot
month.plot <- tbl %>%
group_by(Month, Year) %>%
filter(Year != 2019) %>%
summarise(avgmax = mean(maximum.temperature..degC., na.rm = TRUE)) %>%
ggplot(aes(x = Month,
y = avgmax,
group = Year,
colour = avgmax)) +
geom_point() +
geom_line() +
scale_colour_gradient2(low = "#E1B823", mid = "#DD8033" , high = "#a4111e",
midpoint = 25) +
labs(title = 'Average daily-maximum temperature in Australia over the past 100 years',
subtitle = 'Separated by month',
caption = "Data Source: ACORN-SAT",
y = 'Temperature (°C)',
x = "Year") +
theme_bw() +
theme(panel.border = element_blank(),
axis.line = element_line(colour = "black", size = 0.2),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(size = 6, hjust = 0.5),
legend.text = element_text(size = 6)) +
theme_plot_text +
labs(color = '°C', size = 6)
To animate this plot, we will use “transition states” from package gganimate. This function attributes values of a dimension (in this case, year) to an individual frame. We can then tweak parameters of the animation (length of transitions, whether the plot animation loops, etc)
month.plot <- month.plot +
transition_states(states = Year,
transition_length = 3, # relative length of transistion
state_length = 1, # relative length of pause at the states
wrap = TRUE) + # loops the animation
labs(subtitle = 'Year: {closest_state}') + # includes dynamic sub-title
shadow_mark()
Finally we can animate & view the plot, setting parameters such as framers per second, total frames in the animation, & number of frames comprising the end pause